# 
# Description:
# This script constructs Tables A1 and A2
# for Appendix A
#
# Requires: terror_pres_wide.dta
#
# Author: Sean Nossek
#
# Date: August 04, 2020

# load packages
if (!require("pacman")) install.packages("pacman")

pacman::p_load(ggplot2, tidyverse, readstata13, readxl, maps, sf, tmap, xtable)

# Ready Descriptives for Tables A1-A2

setwd("C:/Users/Sean/Dropbox/Eran/Terrorism_Left/Paper/Revisions/Revised Paper/Replication/Paper Data")

terror <- read.dta13("terror_pres_wide.dta") %>%
  mutate(motive2 = ifelse(motive2 == "Mormon", "Religious", motive2),
         motive2 = ifelse(motive2 == "", "Unknown", motive2),
         motive1 = ifelse(motive1 == "Religious", "Unknown", motive1),
         motive1 = ifelse(motive1 == "", "Unknown", motive1))

#attacktype1_txt
terror_1 <- terror %>% 
  group_by(attacktype1_txt) %>% 
  summarize(count = n(), 
            perc = (count / sum(!is.na(terror$attacktype1_txt))) * 100,
            perc_success = (sum(success == 1) / count) * 100) %>%
  rename(type = attacktype1_txt) %>% 
  mutate(cat = "Attack Type")

terror_1_sub <- terror %>%
  filter(success == 1) %>%
  group_by(attacktype1_txt) %>%
  summarize(avg_inj = mean(nwound, na.rm = TRUE),
            avg_kill = mean(nkill, na.rm = TRUE),
            avg_damage = mean(as.numeric(propvalue), na.rm = TRUE)) %>%
  select(-attacktype1_txt)

terror_1 <- cbind(terror_1, terror_1_sub) %>%
  select(cat, type, everything())

#target
terror_2 <- terror %>%
  group_by(targtype1_txt) %>%
  summarize(count = n(),
            perc = (count / sum(!is.na(terror$attacktype1_txt))) * 100,
            perc_success = (sum(success == 1) / count) * 100) %>%
  rename(type = targtype1_txt) %>%
  mutate(cat = "Target Type")

terror_2_sub <- terror %>%
  filter(success == 1) %>% 
  group_by(targtype1_txt) %>%
  summarize(avg_inj = mean(nwound, na.rm = TRUE),
            avg_kill = mean(nkill, na.rm = TRUE),
            avg_damage = mean(as.numeric(propvalue), na.rm = TRUE)) %>%
  select(-targtype1_txt)

terror_2 <- cbind(terror_2, terror_2_sub) %>%
  select(cat, type, everything())

#weapon
terror_3 <- terror %>%
  group_by(weaptype1_txt) %>%
  summarize(count = n(),
            perc = (count / sum(!is.na(terror$attacktype1_txt))) * 100,
            perc_success = (sum(success == 1) / count) * 100) %>%
  rename(type = weaptype1_txt) %>%
  mutate(cat = "Weapon Type")

terror_3_sub <- terror %>% 
  filter(success == 1) %>%
  group_by(weaptype1_txt) %>%
  summarize(avg_inj = mean(nwound, na.rm = TRUE), 
            avg_kill = mean(nkill, na.rm = TRUE), 
            avg_damage = mean(as.numeric(propvalue), na.rm = TRUE)) %>% 
  rename(type = weaptype1_txt)

terror_3 <- merge(terror_3, terror_3_sub, by = "type", all.x = TRUE) %>% 
  select(cat, type, everything())

# Lone Wolf

terror_4 <- terror %>%
  filter(nperps == 1) %>%
  summarize(count = n(), perc = count / 2381,
            perc_success = (sum(success == 1) / count) * 100) %>%
  mutate(cat = "Operation", type = "Lone wolf")

terror_4_sub <- terror %>%
  filter(success == 1 & nperps == 1) %>%
  summarize(avg_inj = mean(nwound, na.rm = TRUE),
            avg_kill = mean(nkill, na.rm = TRUE),
            avg_damage = mean(as.numeric(propvalue), na.rm = TRUE)) %>%
  mutate(type = "NA")

terror_4 <- merge(terror_4, terror_4_sub, by = "type", all.x = TRUE) %>%
  select(cat, type, everything())

# Mult attacks

terror_5 <- terror %>%
  filter(multiple == 1) %>% summarize(count = n(), perc = count / 2381,
                                      perc_success = (sum(success == 1) / count) * 100) %>%
  mutate(cat = "Operation", type = "Multiple Attacks")

terror_5_sub <- terror %>%
  filter(success == 1 & multiple == 1) %>%
  summarize(avg_inj = mean(nwound, na.rm = TRUE),
            avg_kill = mean(nkill, na.rm = TRUE),
            avg_damage = mean(as.numeric(propvalue), na.rm = TRUE)) %>%
  mutate(type = "NA")

terror_5 <- merge(terror_5, terror_5_sub, by = "type", all.x = TRUE) %>%
  select(cat, type, everything())

# non US target

terror_6 <- terror %>% 
  filter(natlty1 != 217) %>%
  summarize(count = n(),
            perc = count / 2381,
            perc_success = (sum(success == 1) / count) * 100) %>%
  mutate(cat = "Operation", type = "Non-US Target")

terror_6_sub <- terror %>%
  filter(success == 1 & natlty1 != 217) %>%
  summarize(avg_inj = mean(nwound, na.rm = TRUE),
            avg_kill = mean(nkill, na.rm = TRUE),
            avg_damage = mean(as.numeric(propvalue), na.rm = TRUE)) %>%
  mutate(type = "NA")

terror_6 <- merge(terror_6, terror_6_sub, by = "type", all.x = TRUE) %>%
  select(cat, type, everything())

#intl logistics

terror_7 <- terror %>% 
  filter(INT_LOG == 1) %>%
  summarize(count = n(), 
            perc = count / 2381, 
            perc_success = (sum(success == 1) / count) * 100) %>%
  mutate(cat = "Operation", type = "Logistics International")

terror_7_sub <- terror %>%
  filter(success == 1 & INT_LOG == 1) %>%
  summarize(avg_inj = mean(nwound, na.rm = TRUE),
            avg_kill = mean(nkill, na.rm = TRUE),
            avg_damage = mean(as.numeric(propvalue), na.rm = TRUE)) %>%
  mutate(type = "NA")

terror_7 <- merge(terror_7, terror_7_sub, by = "type", all.x = TRUE) %>% 
  select(cat, type, everything())

# all
terror_8 <- terror %>%
  summarize(count = n(),
            perc = NA,
            perc_success = (sum(success == 1) / count) * 100) %>%
  mutate(cat = "All", type = "NA")

terror_8_sub <- terror %>%
  filter(success == 1) %>% 
  summarize(avg_inj = mean(nwound, na.rm = TRUE),
            avg_kill = mean(nkill, na.rm = TRUE),
            avg_damage = mean(as.numeric(propvalue), na.rm = TRUE)) %>%
  mutate(type = "NA")

terror_8 <- merge(terror_8, terror_8_sub, by = "type", all.x = TRUE) %>%
  select(cat, type, everything())

# bind together to output as table

terror_stats <- rbind(terror_1, terror_2, terror_3, terror_4, terror_5, terror_6, terror_7, terror_8)

terror <- terror %>%
  group_by(motive1, motive2) %>%
  mutate(temp = n())

terror$motive2[terror$temp < 2] <- "Other"

terror <- terror %>%
  select(-temp)

# summarize by motives

terror_motives <- terror %>%
  select(year, success, motive1, motive2) %>%
  ungroup() %>%
  group_by(motive1) %>% 
  mutate(cat_count = n()) %>% 
  ungroup() %>%
  group_by(motive1, motive2) %>%
  mutate(count = n(),
         perc = count / cat_count,
         perc_success = (sum(success == 1) / count) * 100) %>%
  select(-c(year, success, cat_count)) %>%
  distinct()

terror_motives_1 <- terror %>%
  select(year, success, motive1, motive2) %>%
  group_by(motive1) %>%
  summarize(motive2 = NA,
            count = n(), 
            perc = count / 2639,
            perc_success = (sum(success == 1) / count) * 100)

terror_motives <- bind_rows(terror_motives, terror_motives_1) %>%
  arrange(motive1, desc(count))

stats_sub <- terror_stats %>%
  select(-c("avg_inj","avg_kill","avg_damage")) %>%
  arrange(cat, desc(count))

# Table A1
print(xtable(stats_sub, type = latex), include.rownames = FALSE)

# Table A2
print(xtable(terror_motives, type = latex), include.rownames = FALSE)
